package org.tof.hotel.servlet;

import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

enum Actions {
    SIGNIN,
    REGISTRATION,
    REMIND,
    GETADMIN,
    CREATEADMIN,
    INDEX
}

public class Controller extends HttpServlet{
    static Logger logger = Logger.getLogger(Controller.class);

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            switch (Actions.valueOf(getAction(req))){
                case SIGNIN:
                    getServletContext().getRequestDispatcher("/jsp/signin.jsp").forward(req,resp);
                    break;
                case REGISTRATION:
                    new Registration().run(req, resp);
                    getServletContext().getRequestDispatcher("/jsp/registration.jsp").forward(req,resp);
                    break;
                case REMIND:
                    getServletContext().getRequestDispatcher("/jsp/remind.jsp").forward(req,resp);
                    break;
                case GETADMIN:
                    break;
                case CREATEADMIN:
                    new CreateAdmin().execute(req,resp);
                    getServletContext().getRequestDispatcher("/jsp/createAdmin.jsp").forward(req,resp);
                    break;
                case INDEX:
                    break;
                default:
                    break;
            }
        } catch (ClassNotFoundException e) {
            logger.error(e.toString(), e);
        }
        getServletContext().getRequestDispatcher("/createAdmin.jsp").forward(req, resp);
    }

    String getAction(HttpServletRequest req){
        return req.getRequestURI().toString().replace("/","").replace(".do","").toUpperCase();
    }
}
